<?php
//------------------------------------------------------------ Contraintes d'accès de la page

	// on vérifie que cette page est appelée à partir de l'index et que l'utilisateur a le droit de consultation
	if(isset($_SESSION) && ($GLOBALS['USER']['level'] >= 2 || $GLOBALS['USER']['admin'])){
	
		// l'identifiant de la donnée à supprimer est indiqué par un entier
		if(isset($_GET['id']) && numericInt($_GET['id'])){
		
			$nb_prets = valueExists((int)$_GET['id'], "CODEPRETSORTANT", "pret_sortant", $connexion);
			
			if($nb_prets == 1){ // le prêt à afficher existe et son identifiant est unique
	
?>
<?php
/********************************* Règles :

	- id doit désigner un seul prêt
	- Le prêt doit exister dans la BDD

*****************************************/

	// récupération des informations du prêt
	$query_recuperation_pret = "SELECT so.DATESORTIE AS dateSortie, en.DATEENTREE AS dateRetour, p.DATERETOUR AS dateRetourPrevu, st.CODESTRUCTURE AS idStructure, st.NOMSTRUCTURE AS structure, com.NOMCOMMUNE AS commune, pays.NOMPAYS AS pays, st.CODEJB AS numcor, so.CODESACHETGRAINE AS graines, so.CODESEMIS AS semis, so.CODETOUFFE AS touffe, sa.CODESACHETGRAINE AS idGraines, sa.REFERENCESACHETGRAINES AS refGraines, se.CODESEMIS AS idSemis, se.REFERENCESEMIS AS refSemis, tou.CODETOUFFE AS idTouffe, tou.REFERENCETOUFFE AS refTouffe, f.NOMFAMILLE AS famille, g.NOMGENRE AS genre, e.NOMESPECE AS espece, sesp.NOMSOUSESPECE AS sous_espece, v.NOMVARIETE AS variete, p.COMMENTAIRE AS commentaire ";
	$query_recuperation_pret .= "FROM pret_sortant p ";
	$query_recuperation_pret .= "INNER JOIN structure st ON st.CODESTRUCTURE = p.CODESTRUCTURE ";
	$query_recuperation_pret .= "INNER JOIN commune com INNER JOIN pays ON com.CODEPAYS = pays.CODEPAYS AND st.CODECOMMUNE = com.CODECOMMUNE ";
	$query_recuperation_pret .= "INNER JOIN sortie so ON so.CODEPRETSORTANT = p.CODEPRETSORTANT ";
	$query_recuperation_pret .= "LEFT JOIN entree en ON en.CODEPRETSORTANT = p.CODEPRETSORTANT ";
	$query_recuperation_pret .= "LEFT JOIN sachet_graines sa ON so.CODESACHETGRAINE = sa.CODESACHETGRAINE ";
	$query_recuperation_pret .= "LEFT JOIN semis se ON so.CODESEMIS = se.CODESEMIS ";
	$query_recuperation_pret .= "LEFT JOIN touffe tou ON so.CODETOUFFE = tou.CODETOUFFE ";
	$query_recuperation_pret .= "LEFT JOIN taxon t ON t.CODETAXON = sa.CODETAXON OR t.CODETAXON = se.CODETAXON OR t.CODETAXON = tou.CODETAXON ";
	$query_recuperation_pret .= "LEFT JOIN est_de_famille l1 INNER JOIN famille f ON f.CODEFAMILLE = l1.CODEFAMILLE ON l1.CODETAXON = t.CODETAXON AND l1.CODECLASSIFICATION = t.CODECLASSIFICATION ";
	$query_recuperation_pret .= "LEFT JOIN appartient_a_genre l2 INNER JOIN genre g ON g.CODEGENRE = l2.CODEGENRE ON l2.CODETAXON = t.CODETAXON AND l2.CODECLASSIFICATION = t.CODECLASSIFICATION ";
	$query_recuperation_pret .= "LEFT JOIN est_de_espece l3 INNER JOIN espece e ON e.CODEESPECE = l3.CODEESPECE ON l3.CODETAXON = t.CODETAXON AND l3.CODECLASSIFICATION = t.CODECLASSIFICATION ";
	$query_recuperation_pret .= "LEFT JOIN est_de_sous_espece l33 INNER JOIN sous_espece sesp ON sesp.CODESOUSESPECE = l33.CODESOUSESPECE ON l33.CODETAXON = t.CODETAXON AND l33.CODECLASSIFICATION = t.CODECLASSIFICATION ";
	$query_recuperation_pret .= "LEFT JOIN a_comme_variete l4 INNER JOIN variete v ON v.CODEVARIETE = l4.CODEVARIETE ON l4.CODETAXON = t.CODETAXON AND l4.CODECLASSIFICATION = t.CODECLASSIFICATION ";
	$query_recuperation_pret.= "WHERE p.CODEPRETSORTANT = ".$_GET['id']." ";
	
	$result_recuperation_pret = mysql_query($query_recuperation_pret, $connexion) or logError("RECUPERATION PRET SORTANT-".$query_recuperation_pret."-".mysql_error());
	
	$tab_recuperation_pret = mysql_fetch_assoc($result_recuperation_pret);
	
	mysql_free_result($result_recuperation_pret);
	
?>
<h3>Détail d'un prêt du jardin botanique</h3>
<table class="fiche" style="width: 100%;">
	<tr>
		<th colspan="2">Dates</th>
	</tr>
	<tr>
		<td style="width: 27%" class="etiquette">Date:</td>
		<td style="width: 73%"><?php echo dateFr($tab_recuperation_pret['dateSortie']); ?></td>
	</tr>
	<tr>
		<td class="etiquette">Retour:</td>
		<td>
			<?php 
				if(!is_null($tab_recuperation_pret['dateRetour'])){
					echo "Rendu le ".dateFr($tab_recuperation_pret['dateRetour']);
					if(!is_null($tab_recuperation_pret['dateRetourPrevu'])){
						echo "<br /><i>(Prévu le ".dateFr($tab_recuperation_pret['dateRetourPrevu']).")";
					}
				}else if(!is_null($tab_recuperation_pret['dateRetourPrevu'])){
					echo "Prévu le ".dateFr($tab_recuperation_pret['dateRetourPrevu'])."";
				}else{
					echo "<i>aucune indication</i>";
				}
			?>
		</td>
	</tr>
	<tr>
		<th colspan="2">Structure</th>
	</tr>
	<tr>
		<td class="etiquette">Prêté par:</td>
		<td><?php echo "<a href=\"popup.php?p=gestion/structure_affiche_popup&amp;id=".$tab_recuperation_pret['idStructure']."\" onclick=\"popup(this.href, 400, 500); return false;\">(".$tab_recuperation_pret['numcor'].") ".htmlentities($tab_recuperation_pret['structure'], ENT_NOQUOTES, "UTF-8")."</a> <br /> <b> ".htmlentities(Capitalise($tab_recuperation_pret['commune']), ENT_NOQUOTES, "UTF-8")." ".htmlentities(strtoupper($tab_recuperation_pret['pays']), ENT_NOQUOTES, "UTF-8")."</b>"; ?></td>
	</tr>
	<tr>
		<th colspan="2">Végétal</th>
	</tr>
	<tr>
		<td colspan="2">
			<?php
				if(isset($tab_recuperation_pret['graines'])){
					echo "<b><a href=\"popup.php?p=gestion/sachetgraines_affiche_popup&amp;id=".$tab_recuperation_pret['idGraines']."\" onclick=\"popup(this.href, 640, 480); return false;\">Sachet de graines";
					if(!is_null($tab_recuperation_pret['refGraines'])){
						echo " (n° ent. ".htmlentities($tab_recuperation_pret['refGraines'], ENT_NOQUOTES, "UTF-8").")";
					}
					echo "</a></b>";
				}else if(isset($tab_recuperation_pret['semis'])){
					echo "<b><a href=\"popup.php?p=gestion/semis_affiche_popup&amp;id=".$tab_recuperation_pret['idSemis']."\" onclick=\"popup(this.href, 640, 480); return false;\">Semis";
					if(!is_null($tab_recuperation_pret['refSemis'])){
						echo " (n° ent. ".htmlentities($tab_recuperation_pret['refSemis'], ENT_NOQUOTES, "UTF-8").")";
					}
					echo "</a></b>";
				}else if(isset($tab_recuperation_pret['touffe'])){
					echo "<b><a href=\"popup.php?p=gestion/touffe_affiche_popup&amp;id=".$tab_recuperation_pret['idTouffe']."\" onclick=\"popup(this.href, 640, 480); return false;\">Touffe";
					if(!is_null($tab_recuperation_pret['refTouffe'])){
						echo " (n° ent. ".htmlentities($tab_recuperation_pret['refTouffe'], ENT_NOQUOTES, "UTF-8").")";
					}
					echo "</a></b>";
				}
				echo " :<br />";
				echo "(".htmlentities(capitalise($tab_recuperation_pret['famille']), ENT_NOQUOTES, "UTF-8").") <b>".htmlentities(capitalise($tab_recuperation_pret['genre']), ENT_NOQUOTES, "UTF-8")." ".htmlentities($tab_recuperation_pret['espece'], ENT_NOQUOTES, "UTF-8")." ".htmlentities($tab_recuperation_pret['sous_espece'], ENT_NOQUOTES, "UTF-8")."</b> <i>".htmlentities($tab_recuperation_pret['variete'], ENT_NOQUOTES, "UTF-8")."</i>";
			?>
		</td>
	</tr>
	<tr>
		<th colspan="2">Commentaire</th>
	</tr>
	<tr>
		<td class="texte" colspan="2"><?php echo nl2br(htmlentities($tab_recuperation_pret['commentaire'], ENT_NOQUOTES, "UTF-8")); ?></td>
	</tr>
</table>
<?php
//------------------------------------------------------------ Accès refusé à la page

			}else{
				afficherStatut("Le prêt n'existe pas", 3); // appel à la fonction afficherStatut() définie dans fonctions.php
			}

		}else{
			afficherStatut("Le prêt n'est pas ou mal indiqué", 3); // appel à la fonction afficherStatut() définie dans fonctions.php
		}

	}else{
		if(isset($_SESSION)){
			include("pages/401.htm");
		}
	}

?>